<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>订单页面</title>
</head>
<body>
	<%@include file="/WEB-INF/jsp/top.jsp" %>
	<h5><a href="${pageContext.request.contextPath }/index2">回到主页</a></h5>
	<h1>这是订单页面</h1>
<div>
		<button type="button" onclick="deleteBatch()">批量删除</button>
	</div>
	<div><table border="2" id="tab1">
		<tr><th><input type='checkbox' id='allCheckbox' onclick='allCheck()'>全选
			</th><th>订单号</th><th>订单时间</th><th>订单总额</th><th>订单状态</th><th>订单地址</th><th>操作</th></tr>
	</table></div>
	<div id="div2"></div>
	<div id="div1"></div>
</body>

<script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/jquery-3.5.1.js"></script>
<script type="text/javascript">
	$(function(){
		start(1);
	});
	var jsonObj={"pageNum":1};
	function start(start){
		jsonObj.pageNum=start;
		$.ajax({
			type:"post",
			data:jsonObj,
			url:"${pageContext.request.contextPath}/orders/selectAllOrdersByUid",
			success:function(result){
				var object="<tr><th><input type='checkbox' id='allCheckbox' onclick='allCheck()'>全选"
					+"</th><th>订单号</th><th>订单时间</th><th>订单总额</th><th>订单状态</th><th>订单地址</th><th>订单项</th><th>操作</th></tr>";
				$.each(result.list,function(i,n){
					object+="<tr><td><input type='checkbox' id='"+(n.oid)*1000+"' name='"+n.count+"' nonce='"+n.total+"'></td>"//c:if不起作用了，以后用数据库的
						+"<td>"+n.oid+"</td><td>"+n.orderTime+"</td><td>"+n.total
						+"</td><td>"+n.state+"</td><td>"+n.address+"</td><td>"
					$.each(n.orderItems,function(j,o){
							object+="<a href='${pageContext.request.contextPath}/store/store?id="+o.sid+"'>";
							if(o.sid%2==0){
								object+="<img alt='沙发' src='${pageContext.request.contextPath }/statics/img/shafa.JPG' height='50' width='75'></a>";
							}else{
								object+="<img alt='马桶' src='${pageContext.request.contextPath }/statics/img/matong.JPG' height='50' width='75'></a>";
							}
								object+="(商品数量="+o.count+",商品小计="+o.subtotal+",商品id="+o.sid+")";
							if(n.state==3 && o.state==0){
								/* object+="<button onclick='pingjia("+n.oid+","+o.sid+")'>评价</button>"; */
								object+="<a href='${pageContext.request.contextPath}/pingjia/pingjiaa?oid="+n.oid+"&sid="+o.sid+"'>评价</a>";
							}
							if(n.state==3 && o.state==1){
								object+="(已评价)";
							}
							object+="<br/>";
					});	
						if(n.state==0){
							object+="</td><td><a href='${pageContext.request.contextPath}/orders/fukuan?oid="+n.oid+"&total="+n.total+"'>付款</a></td><tr>";
						}else if(n.state==1){
							object+="</td><td><a href='#' onclick='caozuo("+n.oid+")'>催卖家发货</a></td><tr>";
						}else if(n.state==2){
							object+="</td><td><a href='#' onclick='caozuo("+n.oid+")'>确认收货</a></td><tr>";
						}else{
							var flag=true;
							$.each(n.orderItems,function(j,o){
								if(o.state==0){
									flag=false;
								}
							});
							if(flag){
								object+="</td><td><a href='#' onclick='deleteOne("+n.oid+")'>删除订单</a></td><tr>";
							}else{
								object+="</td><td>已确认收货</td><tr>";
							}
						}
				});	
				$("#tab1").html(object);
				
				var object1="当前第"+result.pageNum+"页，总共"+result.pages+"页，总共"+result.total+"条记录";
				$("#div2").html(object1);
				//为了删除数据回到原来页面，而不是回到首页
				$("#div2").prop("is",result.pageNum);
				$("#div2").prop("nonce",result.size);
				
				
				var object2="";
				if(result.isFirstPage){
					object2+="<a>首页</a>";
				}else{
					object2+="<a href='#' onclick='pageChange(1)'>首页</a>";
				}
				if(result.hasPreviousPage){
					object2+="<a href='#' onclick='pageChange("+(result.pageNum-1)+")'>&laquo;</a>";
				}
				$.each(result.navigatepageNums,function(i,n){
					if(n==result.pageNum){
						object2+="<a>"+n+"</a>";
					}else{
						object2+="<a href='#' onclick='pageChange("+n+")'>"+n+"</a>";
					}
				});	
				if(result.hasNextPage){
					object2+="<a href='#' onclick='pageChange("+(result.pageNum+1)+")'>&raquo;</a>";
				}
				if(result.isLastPage){
					object2+="<a>末页</a>";
				}else{
					object2+="<a href='#' onclick='pageChange("+result.pages+")'>末页</a>";
				}
				$("#div1").html(object2);
			}
		});
	}
	
	function pageChange(currentPage){
		start(currentPage);
	}

	function allCheck(){//注意！！！动态的不能用click事件，还有用到了dom的方法需要把jQuery对象转换成dom对象
		$("table tr td input[type='checkbox']").prop("checked",$("#allCheckbox")[0].checked);
	}

	function deleteOne(suibian){
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/orders/deleteBatch",
			data:{
				["ordersList[0].oid"]:suibian
			},
			success:function(result){
				if(result>0){
					if($("#div2")[0].nonce==result){//当前页数量等于删除的记录数
						if($("#div2")[0].is==1){
							start(1);
						}else{
							start($("#div2")[0].is-1);
						}
					}else{
						start($("#div2")[0].is);
					}
					window.location.href="${pageContext.request.contextPath}/orders/orderss";
				}
				if(result==-1){
					alert("用户已取消删除");
				}
			}
		});
	}

	function deleteBatch(){
		var all=$("table tr td input:checked");
		if(all.length==0){
			alert("没有要删除的对象，请重新选择");
			return false;
		}
		var jsonObj2={};
		$.each(all,function(i,n){
			jsonObj2["ordersList["+i+"].oid"]=(n.id)/1000;
		});
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/orders/deleteBatch",
			data:jsonObj2,
			success:function(result){
				if(result>0){
					alert("删除"+result+"条记录成功");
					if($("#div2")[0].nonce==result){//当前页数量等于删除的记录数
						if($("#div2")[0].is==1){
							start(1);
						}else{
							start($("#div2")[0].is-1);
						}
					}else{
						start($("#div2")[0].is);
					}
					window.location.href="${pageContext.request.contextPath}/orders/orderss";
				}
				if(result==-1){
					alert("用户已取消删除");
				}
			}
		});
	}

	$("#logOut").click(function(){
		window.location.href="${pageContext.request.contextPath}/logout";
	});

	/* function js(s1,s2,s3){
		var cou=Number(document.getElementById("cou").value);
		var js=Number(document.getElementById("js").value);
		if(document.getElementById(s1*1000).checked){
			//$("#js").val()=s2;
			document.getElementById("cou").value=cou+Number(s2);
			document.getElementById("js").value=js+Number(s3);
			//document.getElementById("js").innerHTML=s3;
		}else{
			document.getElementById("cou").value=cou-Number(s2);
			document.getElementById("js").value=js-Number(s3);
		}
	} */

	function caozuo(suibian){
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/orders/caozuo",
			data:{"oid":suibian},
			success:function(result){
				if(result==2){
					alert("已发货");
				}else{
					alert("已确认收货");
				}
				start(1);
			}
		});
	}

	function pingjia(sui,bian){
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/pingjia/pingjia",
			data:{"oid":sui,"sid":bian},
			success:function(result){
				if(result==1){
					alert("已评价");
					return false;
				}
				window.location.href="${pageContext.request.contextPath}/pingjia/pingjiaa?oid="+sui+"&sid="+bian;
			}
		});
	}
</script>

</html>